package cn.gok.homework;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

class Book{
    private int bid;
    private String name;
    private float price;

    public Book(int bid, String name, float price) {
        this.bid = bid;
        this.name = name;
        this.price = price;
    }
    public void sell(){
        System.out.println(this.name+"被卖掉了！");
    }
    private void sell(float price,String name){
        System.out.println(this.name+"以"+price+"元卖给了"+name);
    }
    @Override
    public String toString() {
        return "Book{" +
                "bid=" + bid +
                ", name='" + name + '\'' +
                ", price=" + price +
                '}';
    }
}
public class HomeWork {
    public static void main(String[] args) throws NoSuchMethodException, IllegalAccessException, InstantiationException, InvocationTargetException {
        作业();
    }

    private static void 作业() throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
        Class<Book> c = Book.class;
        //获得其全构造
        Constructor<Book> con = c.getDeclaredConstructor(int.class,String.class,float.class);
        //创建全构造的实例
        Book book = con.newInstance(1, "战神记", 38f);
        System.out.println(book);
        //通过反射，破坏其封装性
        Method m1 = c.getDeclaredMethod("sell");
        m1.setAccessible(true);
        m1.invoke(book);

        Method m2 = c.getDeclaredMethod("sell", float.class, String.class);
        m2.setAccessible(true);
        m2.invoke(book,49f,"小啊giao");
    }
}
